package com.pochogastos.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasypt.util.password.BasicPasswordEncryptor;

import com.pochogastos.dao.UsuarioDAO;
import com.pochogastos.model.Usuario;

/**
 * Servlet implementation class NuevoUsuario
 */
public class NuevoUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public NuevoUsuario() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String accion = request.getParameter("accion");
			String forward = "";
			if(request.getSession().getAttribute("ERROR_REGISTRO")!=null)
				request.getSession().removeAttribute("ERROR_REGISTRO");
			if(request.getSession().getAttribute("ERROR_LOGIN")!=null)
				request.getSession().removeAttribute("ERROR_LOGIN");
			
			if(accion == null || accion.equalsIgnoreCase("")){
				forward = "/signup.jsp";
			}else if(accion.equalsIgnoreCase("registrar")){
				
				String nombre = request.getParameter("nombre");
				String mail = request.getParameter("email");
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String passwordConfirm = request.getParameter("passwordConfirm");
				
				if("".equalsIgnoreCase(nombre) || "".equalsIgnoreCase(mail) || "".equalsIgnoreCase(username) || 
						"".equalsIgnoreCase(password) || "".equalsIgnoreCase(passwordConfirm)){
					forward = "/NuevoUsuario";
					request.getSession().setAttribute("ERROR_REGISTRO", "Todos los campos deben estar completos");
				}else if(!password.equalsIgnoreCase(passwordConfirm)){
					forward = "/NuevoUsuario";
					request.getSession().setAttribute("ERROR_REGISTRO", "Las passwords no coinciden");
				}else{
					BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
					String encPass = passwordEncryptor.encryptPassword(password);
					Usuario usuario = UsuarioDAO.getInstance().altaUsuario(nombre,username,encPass,mail);
					request.getSession().setAttribute("USUARIO", usuario);
					forward="/Home";
				}
			}
			RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			if(request.getSession().getAttribute("ERROR_LOGIN")!=null)
				request.getSession().removeAttribute("ERROR_LOGIN");
			
			String accion = request.getParameter("accion");
			String forward = "";
			
			if(accion == null || accion.equalsIgnoreCase("")){
				forward = "/signup.jsp";
			}else if(accion.equalsIgnoreCase("registrar")){
				
				String nombre = request.getParameter("nombre");
				String mail = request.getParameter("email");
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				String passwordConfirm = request.getParameter("passwordConfirm");
				
				if("".equalsIgnoreCase(nombre) || "".equalsIgnoreCase(mail) || "".equalsIgnoreCase(username) || 
						"".equalsIgnoreCase(password) || "".equalsIgnoreCase(passwordConfirm)){
					forward = "/NuevoUsuario?accion=";
					request.getSession().setAttribute("ERROR_REGISTRO", "Todos los campos deben estar completos");
				}else if(!password.equalsIgnoreCase(passwordConfirm)){
					forward = "/NuevoUsuario?accion=";
					request.getSession().setAttribute("ERROR_REGISTRO", "Las passwords no coinciden");
				}else{
					BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
					String encPass = passwordEncryptor.encryptPassword(password);
					Usuario usuario = UsuarioDAO.getInstance().altaUsuario(nombre,username,encPass,mail);
					request.getSession().setAttribute("USUARIO", usuario);
					forward="/Home";
				}
			}
			RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);
			rd.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

}
